package algorithm.search;

import java.util.ArrayList;

public class BasicAlgorithm {
    public static void main(String[] args) {
        Integer[] integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 11, 12};
        int user = 10;
        //返回是否存在
        boolean result = basicsearch(integer, user);
        System.out.println(result);
        //返回索引位置
        int index = basicsearchindex(integer, user);
        System.out.println(index);
        //返回所有索引的位置
        ArrayList<Integer> everyindex = basicsearcheveryindex(integer, user);
        System.out.println(everyindex);

    }

    //基本查找，一个一个进行查找，返回是否存在
    public static boolean basicsearch(Integer[] integer, int user) {
        for (int i = 0; i < integer.length; i++) {
            if (user == integer[i]) {
                return true;
            }
        }
        return false;
    }

    //基本查找，一个一个进行查找，返回索引位置,不考虑重复元素
    public static int basicsearchindex(Integer[] integer, int user) {
        int index = -1;
        for (int i = 0; i < integer.length; i++) {
            if (user == integer[i]) {
                index = i;
                return index;
            }
        }
        return index;
    }

    //基本查找，一个一个进行查找，返回索引位置
    public static ArrayList<Integer> basicsearcheveryindex(Integer[] integer, int user) {
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < integer.length; i++) {
            if (user == integer[i]) {
                list.add(i);
            }
        }
        return list;
    }

}
